<?php

    class CrawlController extends WebController
    {

        public function init(){
            parent::init();
            Yii::import('ext.simple_html_dom');
            Yii::import('ext.TextParser');

            set_time_limit(600);
            ini_set('memory_limit', '512M');
        }
        
        /**
        * http://hehe.vn/hehe/web/crawl/getPage
        * 
        * @param CActiveRecord $f
        * @param CActiveRecord $t
        */
        public function actionGetPage($f = NULL, $t = NULL){
            if(!$f && !$t){
                $image = Image::model()->find(new CDbCriteria(array(
                    'order' => 'id DESC',
                    'condition' => 'crawl = "haivl"',
                )));
                $f = ($image && $image->crawl_page) ? $image->crawl_page : 1;
                $t = $f+6;
            }
            if(!$t) $t = $f;
            
            for($page = $f; $page <= $t; $page++){
                $html = new simple_html_dom('http://www.haivl.com/photos/more?sort=hot&page='.$page);

                foreach($html->find('.photoListItem') as $div){
                    $this->_insertHaivl($div->getAttribute('data-id'), $page);  
                }
                
            } 
        }

        private function _insertHaivl($id, $page, $user_id = NULL){

                $time_start = microtime(true);

                $exist_image = Image::model()->exists("crawl = 'haivl' AND crawl_id = {$id}");
                $exist_video = Video::model()->exists("crawl = 'haivl' AND crawl_id = {$id}");

                if($exist_image || $exist_video) return;
                $data = Image::model()->getHaivlImageData($id);

                if(!$data) continue;

                $time_end = microtime(true);
                $benmark = round($time_end - $time_start, 0);

                if($data['youtube']){

                    $video = new Video();
                    $video->name = $data['name'];
                    $video->alias = TextParser::toSEOString($data['name']);
                    $video->youtube = $data['youtube'];

                    $video->user_id = $user_id ? $user_id : UserInfo::model()->getRandomUserId();
                    $video->status = 'APPROVED';
                    $video->created = $data['created'];
                    $video->crawl = "haivl";
                    $video->crawl_id = $id;
                    $video->benmark = $benmark;

                    $video->setIsNewRecord(TRUE);
                    $video->insert();
                    echo "\n {$image->crawl_page}: video: {$image->crawl_id} > {$image->id}<br/>"; 
                    unset($video); 
                }else{
                    $image = new Image();
                    $image->name = $data['name'];
                    $image->alias = TextParser::toSEOString($data['name']);

                    $image->user_id = $user_id ? $user_id :UserInfo::model()->getRandomUserId();
                    $image->status = 'APPROVED';
                    $image->created = $data['created'];
                    $image->crawl = "haivl";
                    $image->crawl_id = $id;
                    $image->crawl_image = $data['img'];
                    $image->crawl_page = $page;
                    $image->benmark = $benmark;

                    $image->setIsNewRecord(TRUE);
                    $image->insert();
                    echo "\n {$image->crawl_page}: image: {$image->crawl_id} > {$image->id}<br/>"; 
                    unset($image);
                }
                

        }


        /////////////////////// CRAWL truyện cười ////////////////

        /**
        * http://hehe.dev/hehe/web/crawl/jokeVnexpress
        */
        public function actionJokeVnexpress(){
            $this->render('joke_vnexpress', array(
                'title' => 'Crawl joke from vnexpress'
            ));
        }
        
        /**
        * http://hehe.dev/web/crawl/vneTieuPham
        * 
        * @param mixed $d
        */
        public function actionAjaxGetJokeVnexpress($d = ''){
//            $a = array('518e880d745d4','518e8a4567150','518e8ae715ee6','518e8afcb05a5','518e8f8f8877f','518e9dd99a2f2','518e9ddd5af85','518e9de7569e6','518e9deec6455','518e9df011d06','518e9df39d18f','518e9df484015','518e9df571936','518e9df674f6f','518e9df7557f8','518e9df83dd1a','518e9df93af3f','518e9dfa522ba','518e9dfb5857a','518e9dfc85e6a','518e9e02e438a','518e9e045d780','518e9e0547aa4','518e9e061d869','518e9e070f866','518e9e0b56351','518e9e1354ff3','518e9e18b4e73','518e9e1d3a954','518e9e231fd05','518e9e2737356','518e9e2a82858','518e9e388a188','518e9e397bcd2','518e9e3a46b6a','518e9e3b3d6a5','518e9e3c3c090','518e9e3fe8e4d','518e9e40cf7e1','518e9e41d34a0','518e9e43132ee','518e9e440a44f','518e9e44cc36f','518e9e45985a3','518e9e4690e65','518e9e47c59a4','518e9e4b5efc7','518e9e4c937d3','518e9e4da8da8','518e9e4f46bec','518e9e506d470','518e9e5152632','518e9e55a91c0','518e9e56b3853','518e9e57acbc3','518e9e588c575','518e9e5cdcb93','518e9e61e5e43','518e9e632fb5f','518e9e641ea0e','518e9e6523d37','518e9e661c336','518e9e6d68dba','518e9e6f0759a','518e9e7461cbf','518e9e754d146','518e9e766fa9a','518e9e80d250f','518e9e82144b2','518e9e91ed2de','518e9e92ee765','518e9e9405398','518e9e94cb77d','518e9e9882591','518e9e99b802d','518e9e9bd78f1','518e9e9cc3aec','518e9e9dbf946','518e9ea37876a','518e9ea46ddc1','518e9ea551000','518e9ea62389a','518e9ea717fe6','518e9ea7d757b','518e9ea9a025f','518e9eaa88541','518e9eab88212','518e9eac85c0a','518e9eb07226c','518e9eb43ee68','518e9eb7acdb8','518e9eb89e42f','518e9eb980bcf','518e9eba6a376','518e9ebb5f866','518e9ebc52707','518e9ebfade8f','518e9ec12839d','518e9ec22fad3','518e9ec31d8cc','518e9ec3e93da','518e9ec74dd94','518e9ec85529c','518e9ec959870','518e9eca33b26','518e9ecb26fa6','518e9ecc26ff6','518e9ecd296ca','518e9ece1c78e','518e9ed1d0492','518e9ed2cbaf1','518e9ed3c0a5c','518e9ed501eee','518e9ed8d7580','518e9eda37eee','518e9ede40de3','518e9edf3d3fa','518e9eecb4412','518e9eedcd0fa','518e9eeed50a3','518e9ef037468','518e9ef12cd0f','518e9ef59e5b0','518e9efd01aaf','518e9f060ba0f','518e9f0733894','518e9f087ea28','518e9f09b99a9','518e9f0ac9be2','518e9f0bed20d','518e9f0e993b2','518e9f0fe0e82','518e9f1107f54','518e9f150142f','518e9f1608f5d','518e9f172cafa','518e9f18266c6','518e9f192d99c');
//            echo '<pre>a:';print_r(count($a));echo '</pre>';
//            
//            $b = glob("upload/joke/1000/*");
//            echo '<pre>b:';print_r(count($b));echo '</pre>';
//            
//            foreach ($b as $filePath) {

//                $n = str_replace(array('upload/joke/1000/', '.jpg'), '', $filePath);
//                if(!in_array($n, $a)){
//                    unlink($filePath);
//                }

//            }
//            
//            die;
//            $s = 'Thứ tư, 12/5/2004, 04:00 GMT+7';
//            echo '<pre>';print_r($s);echo '</pre>';

//            $s1 = preg_replace('{^.+ (\d+)/(\d+)/(\d+).+$}', '$3-$2-$1', $s);
//            
//            if(preg_match('{^\d{4}\-\d{1,2}\-\d{1,2}$}', $s1.'0')){
//                echo '<pre>';print_r('ok');echo '</pre>';
//            }
//            
//            echo '<pre>';print_r($s1);echo '</pre>';die;

//die;

            Yii::import('ext.Benmark');

            $benmark = new Benmark();

            $lastJoke = Joke::model()->find(array(
                'order' => 'id DESC' 
            ));
            if(!$d){
                $d = $lastJoke ? date('Y/m/d', strtotime($lastJoke->vnexpress_date)) : '2002/11/6';
            }

            Yii::import('ext.TextParser');

            $url = "http://vnexpress.net/gl/cuoi/tieu-pham/?d={$d}";

            $html = new simple_html_dom($url); 

            $crawlCount = 0;
            
            
            
            foreach(array_reverse($html->find('.bgLeftWhite .folder-news')) as $div){
                $key = preg_replace('{^/gl/cuoi/tieu-pham/([\w/-]+)/$}', '$1', strtolower($div->find('a', 0)->href));

                if(Joke::model()->exists("crawl = 'vnexpress' AND crawl_id = :crawl_id", array(
                    ':crawl_id' => $key
                ))) continue;


                $data = array();
                $data['key'] = $key;
                $data['image'] = $div->find('.img-subject', 0) ? 'http://vnexpress.net'.$div->find('.img-subject', 0)->src : NULL;
                $data['url'] = 'http://vnexpress.net'.$div->find('a', 0)->href; // /gl/cuoi/tieu-pham/2002/11/3b9c1f17/


                $dataHtml = new simple_html_dom($data['url']);
                $dataP = array();
                foreach($dataHtml->find('.content p.Normal') as $p){
                    $dataP[] = Myext::textDecode($p->outertext);   
                }
                $content = implode("\n", $dataP);
                list($content) = explode('<p class=Normal align=right>', $content);
                $content = str_replace(' class=Normal', '', $content);

                $data['content'] = Myext::textDecode($content);
                if(!$data['content']) continue;



                $date = explode('|', $div->find('.pDateTime', 0)->plaintext);
                if(!isset($date[1])){


                    $data['vne_date'] = preg_replace('{^.+ (\d+)/(\d+)/(\d+).+$}', '$3-$2-$1', $dataHtml->find('.content .spanTime', 0)->plaintext);
                    
                    if(!preg_match('{^\d{4}\-\d{1,2}\-\d{1,2}$}', $data['vne_date'])){
                        $lastJoke = Joke::model()->find(array(
                            'order' => 'id DESC' 
                        ));
                        $data['vne_date'] = $lastJoke->vnexpress_date;
                    }

                }else{
                    $date = trim($date[1]);
                    if(preg_match('{^\d+/\d+$}', $date)){
                        $date .= '/'.date('Y');
                    }
                    $date = preg_replace('{^(\d+)/(\d+)/(\d+)$}', '$3-$2-$1', $date); //27/11/2002
                    $data['vne_date'] = $date;
                }



                $data['name'] = Myext::textDecode($div->find('.right-fnews a', 0)->innertext);
                $data['alias'] = TextParser::toSEOString($data['name']);
                if(Joke::model()->exists("alias = :alias", array(
                    ':alias' => $data['alias']
                ))) continue;



                $desc = $div->find('h3', 0)->innertext;
                list($desc) = explode('<br', $desc);
                $data['desc'] = Myext::textDecode($desc);
                if(!$data['desc']) continue;

                $dataHtml->clear();
                unset($dataHtml);


                ////////////// upload and insert ////////////////
                $imageName = (string)uniqid();

                $joke = new Joke();
                $joke->name = $data['name'];
                $joke->alias = $data['alias'];
                $joke->image = $data['image'] ? $imageName : NULL;
                $joke->desc = $data['desc'];
                $joke->content = $data['content'];
                $joke->user_id = 1;
                $joke->status = 'DISABLE';
                $joke->crawl = 'vnexpress';
                $joke->crawl_id = $data['key'];
                $joke->crawl_image = $data['image'];
                $joke->vnexpress_date = $data['vne_date'];
                $joke->insert();
                //                echo '<pre>';print_r($joke->attributes);echo '</pre>';

                $imgConf = Yii::app()->params->joke_img;
                Yii::import('ext.wideimage.lib.WideImage');

                if($data['image']){
                    $imgObj = WideImage::load($data['image']);
                    $imgObj = $imgObj->resize($imgConf['cover']['width'], $imgConf['cover']['height'], 'outside', 'down');
                    $imgObj = $imgObj->resizeCanvas($imgConf['cover']['width'], $imgConf['cover']['height'],'center','center', null, 'down');
                    $savePath = $imgConf['path'].$joke->getGroupFolder()."/";
                    Myext::createDir($savePath);
                    $imgObj->saveToFile($savePath.$imageName.'.jpg', 90); 
                }
                unset($data);
                $crawlCount++;
            }
            $html->clear();
            unset($html);

            if($crawlCount == 0){
                $date = new DateTime($d);
                $date->add(new DateInterval('P5D'));
                $nextD = $date->format('Y/m/d');
                return self::actionAjaxGetJokeVnexpress($nextD);
            }

            $log = array(
                'date' => $joke->vnexpress_date,
                'count' => $crawlCount,
                'time' => $benmark->getTime(),
            );

            echo json_encode($log);

            return array($joke->vnexpress_date, $crawlCount);

        }

    }
